//
//  WBOriginalView.swift
//  WeiboTen
//
//  Created by ItHeiMa on 2017/7/9.
//  Copyright © 2017年 itHeima. All rights reserved.
//

import UIKit
import SDWebImage

class WBOriginalView: UIView {
    
    var statusViewModel: WBStatusViewModel? {
        didSet {
//            icon.wb_setImage(urlStr: statusViewModel?.statusModel.user?.avatar_large, placeHolderImage: "avatar_default_big")
            pictureView.statusViewModel = statusViewModel
            
            //在头像下载之前，先设一个默认的图片
            icon.image = UIImage(named: "avatar_default_big")
            
            let iconUrl = URL(string: (statusViewModel?.statusModel.user?.avatar_large)!)
            SDWebImageManager.shared.loadImage(with: iconUrl, options: [], progress: nil, completed: { (downloadImage, _, _, _,_,_) in
                downloadImage?.createCircleImage(color: UIColor.white, size: homeVS.iconSize) {
                    (circleImage) in
                    self.icon.image = circleImage
                }
            })
            
            screenName.text = statusViewModel?.statusModel.user?.screen_name
            textLabel.text = statusViewModel?.statusModel.text
            vipIcon.image = statusViewModel?.vipImage
            createTime.text = statusViewModel?.timeStr
            source.text = statusViewModel?.sourceText
            levelIcon.image = statusViewModel?.levelImage
            
            //判断是否有配图视图， 调整配图视图的约束
            if let pic_urls = statusViewModel?.statusModel.pic_urls, pic_urls.count > 0 {
                pictureView.snp.updateConstraints({ (make) in
                    make.top.equalTo(textLabel.snp.bottom).offset(homeVS.gap)
                    //priority设置约束的优先级，默认值是1000，999就代表优先级要低
                    make.size.equalTo(statusViewModel!.pictureSize).priority(999)
                })
            } else {
                //把pictureView调整到看不见
                pictureView.snp.updateConstraints({ (make) in
                    make.top.equalTo(textLabel.snp.bottom)
                    make.size.equalTo(CGSize.zero).priority(999)
                })
            }
        }
    }
    
    /// 用户的头像
    lazy var icon: UIImageView = UIImageView(imageName: "avatar_default_big")
    
    /// vip的图标
    lazy var vipIcon: UIImageView = UIImageView(imageName: "avatar_enterprise_vip")
    
    /// 皇冠等级的图标
    lazy var levelIcon: UIImageView = UIImageView(imageName: "common_icon_membership_level3")
    
    /// 用户昵称
    lazy var screenName: UILabel = UILabel(text: "娃哈哈", fontSize: 15)
    
    /// 创建时间
    lazy var createTime: UILabel = UILabel(text: "2小时前", textColor: UIColor.lightGray, fontSize: 12)
    
    /// 来源
    lazy var source: UILabel = UILabel(text: "新浪微博", textColor: UIColor.lightGray, fontSize: 12)
    
    /// 微博的正文
    lazy var textLabel: FFLabel = FFLabel(text: "娃哈哈", fontSize: 15)
    
    /// 配图视图
    lazy var pictureView: WBStatusPictureView = WBStatusPictureView()

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.white
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension WBOriginalView {
    func setupUI () {
        //1. 添加子控件
        addSubview(icon)
        addSubview(vipIcon)
        addSubview(levelIcon)
        addSubview(screenName)
        addSubview(createTime)
        addSubview(source)
        addSubview(textLabel)
        addSubview(pictureView)
        
        pictureView.backgroundColor = UIColor.red
        
        //2. 自动布局
        icon.snp.makeConstraints { (make) in
            make.left.top.equalTo(self).offset(homeVS.gap)
            make.size.equalTo(homeVS.iconSize)
        }
        
        vipIcon.snp.makeConstraints { (make) in
            make.left.equalTo(icon.snp.right).offset(-5)
            make.top.equalTo(icon.snp.bottom).offset(-5)
            make.size.equalTo(homeVS.lsize)
        }
        
        screenName.snp.makeConstraints { (make) in
            make.left.equalTo(icon.snp.right).offset(homeVS.gap)
            make.top.equalTo(icon)
        }
        
        levelIcon.snp.makeConstraints { (make) in
            make.left.equalTo(screenName.snp.right).offset(homeVS.gap)
            make.bottom.equalTo(screenName).offset(-3)
            make.size.equalTo(homeVS.lsize)
        }
        
        createTime.snp.makeConstraints { (make) in
            make.left.equalTo(icon.snp.right).offset(homeVS.gap)
            make.bottom.equalTo(icon).offset(0)
        }
        
        source.snp.makeConstraints { (make) in
            make.left.equalTo(createTime.snp.right).offset(homeVS.gap)
            make.bottom.equalTo(createTime)
        }
        
        textLabel.snp.makeConstraints { (make) in
            make.left.equalTo(icon)
            make.right.equalTo(self).offset(-homeVS.gap)
            make.top.equalTo(icon.snp.bottom).offset(homeVS.gap)
        }
        
        pictureView.snp.makeConstraints { (make) in
            make.left.equalTo(textLabel)
            //make.right.equalTo(textLabel)
            make.size.equalTo(CGSize.zero).priority(999)
            make.top.equalTo(textLabel.snp.bottom).offset(homeVS.gap)
            make.bottom.equalTo(self).offset(-homeVS.gap)
        }
        
        //3. 测试textLabel
        //创建一个随机长度的字符串
        var str = "我爱北京天安门"
        let randomInt = Int(arc4random() % 20 + 1)
        for i in 0..<randomInt {
            str += "我爱北京天安门"
        }
        textLabel.text = str
    }
}








